package com.zy.imagineCommunity.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 在SpringBoot中，只能通过创建SpringMVC配置文件来注册拦截器
 */
@Configuration
public class MyWebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    LoginHandlerInterceptor loginHandlerInterceptor;

    @Autowired
    AdminLoginHandlerInterceptor adminLoginHandlerInterceptor;

    //注册拦截器,将登录拦截器配置到容器中
    //.addPathPatterns("/**")表示拦截所有的请求，
    //.excludePathPatterns表示路径不需要拦截哪些路径
    //一定要在这里排除拦截static路径下的静态文件，或者在SpringBoot配置文件上配置，否则页面加载不到样式
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //这里没有拦截所有的请求
        registry.addInterceptor(loginHandlerInterceptor)
                .addPathPatterns("/post")
                .addPathPatterns("/toHotBlogList/**")
                .addPathPatterns("/list/**")
                .addPathPatterns("/friends")
                .addPathPatterns("/blogSearchList")
                .addPathPatterns("/blogDetails")
                .addPathPatterns("/message/**")
                .addPathPatterns("/personalPost")
                .addPathPatterns("/toSystemMessage");

        registry.addInterceptor(adminLoginHandlerInterceptor)
                .addPathPatterns("/managementIndex")
                .addPathPatterns("/managementIndexByType/**")
                .addPathPatterns("/userManagement")
                .addPathPatterns("/blogManagement")
                .addPathPatterns("/postManagement")
                .addPathPatterns("/toCommentManagement")
                .addPathPatterns("/toReplyManagement")
                .addPathPatterns("/userRightsManagement")
                .addPathPatterns("/bannerManagement")
                .addPathPatterns("/toSystemMessageManagement")
                .addPathPatterns("/blogManagementByType/**")
                .addPathPatterns("/toCommentManagementByType/**")
                .addPathPatterns("/toReplyManagementByType/**")
                .addPathPatterns("/userRightsManagementByType/**");

    }

//    /**
//     * addViewControllers：页面跳转
//     */
//    @Override
//    public void addViewControllers(ViewControllerRegistry registry) {
//        registry.addViewController("/").setViewName("forward:/login");
//    }

}
